package lib.com.Service;

import lib.com.Business.component.login.LoginComponent;
import lib.com.Business.dataModel.LoginModel;
import water.com.Ioc.WaterBean;
import water.com.Ioc.WaterInject;
import lib.com.Service.In.Core0001In;
import lib.com.Service.Out.Core0001Out;
import water.com.Log.Log;
import water.com.Service.base.BaseCore;
import water.com.Service.entity.comm.SysHead;

/**
 * Core001: 用户登录接口，主要用于各种用户的登录功能
 */
@WaterBean(type = "Service")
public class Core0001 implements BaseCore<Core0001In,Core0001Out> {

    @WaterInject
    private LoginComponent loginComponent;

    @Override
    public Core0001Out execute(Core0001In core0001In) throws Exception{
        Core0001Out core0001Out = new Core0001Out();
        SysHead sysHead=core0001In.getSysHead();
        LoginModel user = new LoginModel();
        user.setAccount(core0001In.getBody().getAccount());
        user.setPassword(core0001In.getBody().getPassWord());
        user.setUserType(core0001In.getBody().getUserType());
        boolean loginFlag = loginComponent.login(user);
        core0001Out.setBody(new Core0001Out.Body());
        Core0001Out.Body body = core0001Out.getBody();
        if(loginFlag){
            sysHead.setResponseCode("000000");
            body.setLoginFlag("Y");
        }else {
            sysHead.setResponseCode("999999");
            body.setLoginFlag("N");
        }
        core0001Out.setSysHead(sysHead);
        Log.getLog().info("成功调用接口0001!");
        return core0001Out;
    }

    @Override
    public void check(Core0001In core0001In) throws Exception {
        if(core0001In.getBody().getAccount()==null||core0001In.getBody().getAccount().trim().equals("")){
            throw new Exception("check:账号不能为空");
        }
        if(core0001In.getBody().getPassWord()==null||core0001In.getBody().getPassWord().trim().equals("")){
            throw new Exception("check:密码不能为空");
        }
        if(core0001In.getBody().getUserType()==null||core0001In.getBody().getUserType().trim().equals("")){
            throw new Exception("check:用户类型不能为空");
        }
        String userType = core0001In.getBody().getUserType();
        if(!userType.equals("S")&&!userType.equals("N")&&!userType.equals("B")){
            throw new Exception("check:用户类型输入错误");
        }
    }

}
